
local nl = require'luaNumeric'
--local luaFEM2D = require "luaFEM2D"
local unity = require "luaUnity"
require("lfs")

--==============================================================================================================--
--设置默认参数
local CSSModule=require"luaFEM2D"
local FEM2D = CSSModule.ScalarFEM2D;
--==============================================================================================================--

--==============================================================================================================--
--初始化
function FEM2D:init( )
	self.script = [[
	local nl = require'luaNumeric'
	local unity = require "luaUnity"
	self:clear();
	self:set_init_alpha_beta(1,0)
	self:mesh():dmax(5)

	local rad = Geometry.object('rad');
	assert(rad, '读取模型错误')
	self.bbox = rad:bbox();
	local inter = Geometry.NewInterpolate2()
	data = unity.file_to_num_table('data.txt','\t')
	for ii=8,#data-1 do 
		inter:insert( data[ii][1], data[ii][2], data[ii][ 5 ] );
	end
	r=300;
	w=10
	dx = 230;
	arc = Geometry.create_arc( {pts={{-r+dx,0,0},{0+dx,r,0},{r+dx,0,0} }});
	arc1 = Geometry.create_arc( {pts={{-r+dx,0,0},{0+dx,-r,0},{r+dx,0,0} }});
	outer = Geometry.join({arc,arc1})

	local cir = Geometry.create_circle({radius=r,pt={dx,0}});

	local rect = Geometry.create_rect({pt1={-w+dx,-w},pt2={w+dx,w}});

	
	self:add_shape(outer);
	self:add_shape(cir);	
	self:add_shape(rad);	
	--self:add_shape(rect);	
	self:clear_view();
	self:add_object_to_view(outer);
	self:add_object_to_view(rad);
	self:create_mesh()
	mesh = self:mesh()
	------------------------------------------------------------
	--所有三角形信息
	local infos = self:triangles_info();
	trigs_groups_tag = mesh:triangles_groups_tags();
	------------------------------------------------------------
	--所有编组三角形标号
	--把编组内的三角形的fe设置为1
	local trigs_group = trigs_groups_tag[2];
	for kk=1,#trigs_group do
		local info = infos[trigs_group [kk] ];
		center =  info:center()
		val,_ = inter:interpolate(center:x(),center:y());
		info:f( val  )
	end
	------------------------------------------------------------
	------------------------------------------------------------

	self:associate_shape_to_D_BCs(0,0);
	--self:associate_shape_to_D_BCs(2,1);
	--self:associate_shape_to_D_BCs(3,2);
	print( 'start' )
	self:init_matrix();
	self:init_material_info();
	self:fill_matrix();
	self:init_BCs();
	print( 'solve' )
	self:solve();
	print( 'post' )
	self:post_calculate();
	 self:mesh():is_outline(false);
	--Geometry.AddShape( self:mesh() );
	self:add_object_to_view(self:mesh());

	print(self:mesh():points():size())
	
	]];
	self:update_script_to_ui(self.script)
end
--==============================================================================================================--

function FEM2D:create(  )
end

--==============================================================================================================--
function FEM2D:get_border()
	if self.bbox then 
		return self.bbox:xmin(),self.bbox:ymin(),self.bbox:zmin(),self.bbox:xmax(),self.bbox:ymax(),self.bbox:zmax()
	end
	return -1,-1,-1,1,1,1
end
--==============================================================================================================--

--==============================================================================================================--
function FEM2D:calculate()
	self:update_current_widget_index(0);
	--self:create();
	self.script = self:update_script_from_ui();
	--print(self.script)
	self.script_exe = 'function Gallop.FEM2DSover:exec()\n' ..
	self.script .. ' \n end'
	local f = load(self.script_exe)
	f()
	self:exec();
	self:update_current_widget_index(1);
	self:update_border_to_view();
end
--==============================================================================================================--

return FEM2D; 
